package cn.thoughtworks.school.programCenter.util.excel;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class SumPointExcel extends Excel {

    public static Excel init(List<Map<String, String>> data) {
        Excel sumPointExcel = new SumPointExcel();
        sumPointExcel.data = data;
        sumPointExcel.workbook = new HSSFWorkbook();
        return sumPointExcel;
    }

    @Override
    public HSSFWorkbook fillData() {
        HSSFSheet sheet = workbook.createSheet();
        fillColumnsTitle(sheet, Arrays.asList("序号", "姓名", "积分榜"));
        fillBody(sheet);
        return workbook;
    }

    protected void fillBody(HSSFSheet sheet) {
        for (int i = 0; i < data.size(); i++) {
            Map<String, String> studentStatus = data.get(i);
            HSSFRow row = sheet.createRow(i + 1);
            row.setHeight((short) (25 * 20));

            createCell(row, 0, i + 1 + "", getDefaultCellStyle());
            createCell(row, 1, studentStatus.get("name"), getDefaultCellStyle());
            createCell(row, 2, studentStatus.get("sumPoint") + "", getDefaultCellStyle());
        }
    }

    protected void fillColumnsTitle(HSSFSheet sheet, List<String> titles) {
        HSSFRow row = sheet.createRow(0);
        row.setHeight((short) (25 * 25));
        for (int i = 0; i < titles.size(); i++) {
            createCell(row, i, titles.get(i), getDefaultCellStyle());
        }
    }
}
